﻿namespace Sidvall.LogManager.UI.Entities
{
	public sealed class TreeItem : global::Sidvall.LogManager.UI.Entities.Core.BusinessBase
	{
		#region Public Members

		#region ServerManager

		private ServerManager _ServerManager;
		public ServerManager ServerManager
		{
			get
			{
				return _ServerManager;
			}
			set
			{
				_ServerManager = value;
				OnPropertyChanged("ServerManager");
				OnPropertyChanged("LogScopes");
			}
		}

		#endregion
		#region LogScopes

		public LogScopeCollection LogScopes
		{
			get
			{
				return this.ServerManager.LogScopes;
			}
		}

		#endregion

		#region Name

		private string _Name = string.Empty;
		public string Name
		{
			get
			{
				return _Name;
			}
			set
			{
				_Name = value;
				OnPropertyChanged("Name");
			}
		}

		#endregion
		#region FullName

		private string _FullName = string.Empty;
		public string FullName
		{
			get
			{
				return _FullName;
			}
			set
			{
				_FullName = value;
				OnPropertyChanged("FullName");
			}
		}

		#endregion
		#region ServerConnectionId

		private int _ServerConnectionId;
		public int ServerConnectionId
		{
			get
			{
				return _ServerConnectionId;
			}
			set
			{
				_ServerConnectionId = value;
				OnPropertyChanged("ServerConnectionId");
			}
		}

		#endregion
		#region LogScopeId

		private System.Nullable<long> _LogScopeId;
		public System.Nullable<long> LogScopeId
		{
			get
			{
				return _LogScopeId;
			}
			set
			{
				_LogScopeId = value;
				OnPropertyChanged("LogScopeId");
			}
		}

		#endregion
		#region SortOrder

		private string _SortOrder = string.Empty;
		public string SortOrder
		{
			get
			{
				return _SortOrder;
			}
			set
			{
				_SortOrder = value;
				OnPropertyChanged("SortOrder");
			}
		}

		#endregion
		#region IsEnabled

		private bool _IsEnabled = false;
		public bool IsEnabled
		{
			get
			{
				return _IsEnabled;
			}
			set
			{
				_IsEnabled = value;
				OnPropertyChanged("IsEnabled");
			}
		}

		#endregion
		#region IsServer

		private bool _IsServer = false;
		public bool IsServer
		{
			get
			{
				return _IsServer;
			}
			set
			{
				_IsServer = value;
				OnPropertyChanged("IsServer");
			}
		}

		#endregion
		#region IsLogScope

		private bool _IsLogScope = false;
		public bool IsLogScope
		{
			get
			{
				return _IsLogScope;
			}
			set
			{
				_IsLogScope = value;
				OnPropertyChanged("IsLogScope");
			}
		}

		#endregion
		#region TreeItemId

		public string TreeItemId
		{
			get
			{
				return GetTreeItemId(this.ServerConnectionId, this.LogScopeId);
			}
		}

		#endregion
		#region ItemTooltip

		private string _ItemTooltip;
		public string ItemTooltip
		{
			get
			{
				return _ItemTooltip;
			}
			set
			{
				if (string.IsNullOrWhiteSpace(value))
					value = null;
				_ItemTooltip = value;
				OnPropertyChanged("ItemTooltip");
			}
		}

		#endregion

		#region GetSortOrder

		public static string GetSortOrder(int serverConnectionId)
		{
			return GetSortOrder(serverConnectionId, null, null);
		}
		public static string GetSortOrder(int serverConnectionId, long? applicationId)
		{
			return GetSortOrder(serverConnectionId, applicationId, null);
		}
		public static string GetSortOrder(int serverConnectionId, long? applicationId, int? executionOrder)
		{
			string key;

			key = serverConnectionId.ToString().PadLeft(7, '0');
			if (applicationId != null)
				key += "." + applicationId.ToString().PadLeft(7, '0');
			if (executionOrder != null)
				key += "." + executionOrder.ToString().PadLeft(7, '0');
			return key;
		}

		#endregion
		#region GetTreeItemId

		public static string GetTreeItemId(int serverConnectionId)
		{
			return GetTreeItemId(serverConnectionId, null);
		}
		public static string GetTreeItemId(int serverConnectionId, long? applicationId)
		{
			string key;

			key = serverConnectionId.ToString();
			if (applicationId != null)
				key += "." + applicationId.ToString();
			return key;
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			if (this.IsServer)
				return this.Name;
			return this.ServerConnectionId.ToString() + "." + this.Name;
		}

		#endregion

		#endregion
		#region Constructors

		public TreeItem()
		{
		}

		public TreeItem(Sidvall.Data.DataRowState? dataRowState)
			: base(dataRowState)
		{
		}

		#endregion
	}
}